<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>退药管理</title>
</head>
<body>
	<script type="text/javascript">

		$(function() {
			
			//退药记录
			$('#returndrugjl-dg').datagrid({
				url : 'find-returndrugjl',
				pagination : true,
				pageNumber : 1,
				pageSize : 6,
				singleSelect: true,
				pageList : [ 6, 12 ],
				columns : [ [
					{field : 'return_goods_id',					title : '退药单号',		width : 150},
					{field : 'return_goods_head',				title : '负责人',		width : 140},
					{field : 'return_goods_allquantity',		title : '总数量',		width : 140},
					{field : 'return_goods_total',				title : '总金额',		width : 140},
					{field : 'return_goods_time',				title : '退药时间',		width : 150,formatter:comFormatDate},
					{field : 'return_goods_reason',				title : '退药原因',		width : 182},
					{field : 'return_goods_checkpeople',		title : '审核人',		width : 145}
				] ],
				onDblClickRow : function(rowIndex, rowData) {
					$("#returndrugjlxq-win").window("open");
					//给退药单号赋值
				    $("#returndrug-tydh").textbox('setValue',rowData.return_goods_id);
				  	//给退药时间赋值
				  	var s=rowData.return_goods_time;
					var t=CurentTime(s);
				    $("#returndrug-tysj").textbox('setValue', t);
				  	//给退药原因赋值
				    $("#returndrug-yy1").textbox('setValue',rowData.return_goods_reason);
				  	
				  	//获取退药单号
				  	var returngoodsid = $("#returndrug-tydh").textbox("getValue");
				  	
				  //根据退药单号获取
					$.ajax({  
                        url: 'findBy-turndrugjl',  
                        data:{'returngoodsid' :returngoodsid},
                        type: 'POST',  
                        dataType:"json",
					 	success: function (data) {  
					 				//遍历数据前清空datagrid数据
					 				$('#returndrugjlxq-dg').datagrid('loadData',{total:0,rows:[]});
					 				console.log(data.result);
					 				//循环退药商品数据
					                for ( var machine in data.result) {
					                    var row_data = {
					    					'inventory_number' : data.result[machine].inventory_number,
					                        'drug_name' : data.result[machine].drug_name,
					                        'drug_standard' : data.result[machine].drug_standard,
					                        'drug_purchase_price' : data.result[machine].drug_purchase_price,
					                        'return_details_quantity' : data.result[machine].return_details_quantity,
					                        'return_details_jine' : data.result[machine].drug_purchase_price*data.result[machine].return_details_quantity,
					                        'return_details_pici' : data.result[machine].return_details_pici				                        
					                    };
					                    $('#returndrugjlxq-dg').datagrid('appendRow', row_data);
					                }
						     }, 				     
	                        Error: function () {  
	                            alert(Error);  
	                        }                   
	                    });    
				  
				}
			});

			//退药表
			$('#returndrug-dg').datagrid({
				url : 'find-returndrug',
				pagination : true,
				pageNumber : 1,
				pageSize : 3,
				singleSelect: true,
				pageList : [ 3, 6 ],
				columns : [ [
					{field : 'return_goods_id',					title : '退药单号',		width : 170},
					{field : 'return_goods_head',				title : '负责人',		width : 170},
					{field : 'return_goods_allquantity',		title : '总数量',		width : 150},
					{field : 'return_goods_total',				title : '总金额',		width : 150},
					{field : 'return_goods_time',				title : '时间',			width : 170,formatter:comFormatDate},
					{field : 'return_goods_reason',				title : '退药原因',		width : 236},
					{field : 'return_goods_state',				title : '退药状态',		width : 236,hidden:true}
				] ],
				onDblClickRow : function(rowIndex, rowData) {
					$("#returndrug-win").window("open");
					//给退药单号赋值
				    $("#returndrug-tydh1").textbox('setValue',rowData.return_goods_id);
				  	//给退药时间赋值
				  	var s=rowData.return_goods_time;
					var t=CurentTime(s);
				    $("#returndrug-sj").textbox('setValue',t);
				  	//给退药原因赋值
				    $("#returndrug-yy").textbox('setValue',rowData.return_goods_reason);
				  	//给退药状态赋值
				    $("#asdfzt").textbox('setValue',rowData.return_goods_state);
				  	
				  	//获取退药单号
				  	var returngoodsid = $("#returndrug-tydh1").textbox("getValue");
				  	
				  	//根据退药单号获取
					$.ajax({  
                        url: 'findBy-turndrugjl',  
                        data:{'returngoodsid' :returngoodsid},
                        type: 'POST',  
                        dataType:"json",
					 	success: function (data) {  
					 				//遍历数据前清空datagrid数据
					 				$('#returndrugxq-dg').datagrid('loadData',{total:0,rows:[]});
					 				//循环退药商品数据
					                for ( var machine in data.result) {
					                    var row_data = {
					    					'inventory_number' : data.result[machine].inventory_number,
					                        'drug_name' : data.result[machine].drug_name,
					                        'drug_standard' : data.result[machine].drug_standard,
					                        'drug_purchase_price' : data.result[machine].drug_purchase_price,
					                        'return_details_quantity' : data.result[machine].return_details_quantity,
					                        'return_details_jine' : data.result[machine].drug_purchase_price*data.result[machine].return_details_quantity,
					                        'return_details_pici' : data.result[machine].return_details_pici				                        
					                    };
					                    $('#returndrugxq-dg').datagrid('appendRow', row_data);
					                }
						     }, 				     
	                        Error: function () {  
	                            alert(Error);  
	                        }                   
	                    });    
				}
			});
			
			//退药表详情
			$('#returndrugxq-dg').datagrid({
				url : '',
				pagination : true,
				pageNumber : 1,
				pageSize : 10,
				singleSelect: true,
				pageList : [ 10, 20 ],
				columns : [ [ 
					{field : 'inventory_number',			title : '药品编号',			width : 147.1}, 
					{field : 'drug_name',					title : '药品名称',		width : 147.1}, 
					{field : 'drug_standard',				title : '规格',			width : 147.1}, 
					{field : 'drug_purchase_price',			title : '单价',			width : 147.1},
					{field : 'return_details_quantity',		title : '退药数量',		width : 147.1},
					{field : 'return_details_jine',			title : '金额',			width : 147.1},
					{field : 'return_details_pici',			title : '药品批次',		width : 145.1}
				] ]
			});

			//退药记录详情
			$('#returndrugjlxq-dg').datagrid({
				url : '',
				pagination : true,
				pageNumber : 1,
				singleSelect: true,
				pageSize :10,
				pageList : [10, 20 ],
				columns : [ [ 
					{field : 'inventory_number',			title : '药品编号',		width : 147.1}, 
					{field : 'drug_name',					title : '药品名称',		width : 147.1}, 
					{field : 'drug_standard',				title : '规格',			width : 147.1}, 
					{field : 'drug_purchase_price',			title : '单价',			width : 147.1},
					{field : 'return_details_quantity',		title : '退药数量',		width : 147.1},
					{field : 'return_details_jine',			title : '金额',			width : 147.1},
					{field : 'return_details_pici',			title : '药品批次',		width : 145.1}
				] ]
			});
			
			//取消入库验收
			$("#hsdfdf").click(function(){
				$("#returndrug-win").window("close");
			});
			
			//药品退药
			$("#returndrugok").click(function(){
				//获取退货单号
				var returngoid = $("#returndrug-tydh1").textbox("getValue");
				//获取审批人
				var checkpeople = $("#checkpeople").val() ;	
				//获取退药状态
				var asdfzt = $("#asdfzt").val();
				
				$.ajax({  
                    url: 'update-return',  
                    data:{
                    	'returngoid':returngoid,
                    	'checkpeople':checkpeople
                    },
                    type: 'POST',  
                    dataType:"json",
				 	success: function (data) {  						
						$(".drugoneu").text("退药成功");
				 		 //清空文本
				 		$("#DRUGONEu").window('open');
				  	 	//秒之后自动消失
				   		setTimeout(function(){
				    	 	$("#DRUGONEu").window('close');
				    	 	$("#returndrug-win").window("close");
						}, 1500);
				     }, 				     
                    Error: function () {  
                        alert(Error);  
                    }                   
                });    
				
				//获取表格中的详情记录
				var rows = $("#returndrugxq-dg").datagrid("getRows");
				console.log(rows);

				var total=0;
				var money=0;
				
				if(asdfzt==0){
					for (var i = 0; i < rows.length; i++) {					
						//获取库存详号
						var inventorynum = rows[i]["inventory_number"];
						//获取退药数量
						var sum =  rows[i]["return_details_quantity"];							
						//获取库存批次
						var batch = rows[i]["return_details_pici"];							
						$.ajax({  
		                       url: 'update-kucun',  
		                       data:{
		                    	   'inventorynum':inventorynum,
		                    	   'sum':sum
		                       },
		                       type: 'POST',  
		                       dataType:"json",
							   success: function (data) {  
							
							   }, 				     
		                       Error: function () {  
		                           alert(Error);  
		                       }                   
		                 });    
						
						$.ajax({  
		                       url: 'update-inpici',  
		                       data:{
		                    	   'inventorynum':inventorynum,
		                    	   'sum':sum,
		                    	   'batch':batch
		                       },
		                       type: 'POST',  
		                       dataType:"json",
							   success: function (data) {  
							
							   }, 				     
		                       Error: function () {  
		                           alert(Error);  
		                       }                   
		                });     
					}
				}
				
				
				//刷新表格
				$('#returndrug-dg').datagrid('reload');
				$('#returndrugjl-dg').datagrid('reload');
				
			});
			
			// 时间戳
			function CurentTime(){
		  
		        var now = new Date();
		       
		        var year = now.getFullYear();       //年
		        var month = now.getMonth() + 1;     //月
		        var day = now.getDate();            //日
		       
		        var hh = now.getHours();            //时
		        var mm = now.getMinutes();          //分
				var ss = now.getSeconds();			//秒
		       
		        var clock = year + "-";
		       
		        if(month < 10)
		            clock += "0";
		       
		        clock += month + "-";
		       
		        if(day < 10)
		            clock += "0";
		           
		        clock += day + " ";
		       
		        if(hh < 10)
		            clock += "0";
		           
		        clock += hh + ":";
		        if (mm < 10) clock += '0'; 
		        clock += mm + ":"; 
				
		        if (ss < 10) clock += '0'; 
		        clock += ss; 
		        return(clock); 
			}
				
			// 时间戳
			function comFormatDate(date) {
				var day = new Date(date); 
				var Year = 0; 
				var Month = 0; 
				var Day = 0; 
				var CurrentDate = ""; 
				//初始化时间 
				Year= day.getFullYear();//ie火狐下都可以 
				Month= day.getMonth()+1; 
				Day = day.getDate(); 
				Hour = day.getHours(); 
				 Minute = day.getMinutes(); 
				 Second = day.getSeconds(); 
				CurrentDate += Year + "-"; 
				if (Month >= 10 ) { 
					CurrentDate += Month + "-"; 
				}else { 
					CurrentDate += "0" + Month + "-"; 
				} 
				if (Day >= 10 ) { 
					CurrentDate += Day ; 
				}else { 
					CurrentDate += "0" + Day ; 
				} 
				if(Hour>=10){
					CurrentDate +=" "+Hour;
				}else{
					CurrentDate +=" "+"0"+Hour;
				}
				if(Minute>=10){
					CurrentDate +=":"+Minute;
				}else{
					CurrentDate +=":"+"0"+Minute;
				}
				if(Second>=10){
					CurrentDate +=":"+Second;
				}else{
					CurrentDate +=":"+"0"+Second;
				}
				return CurrentDate; 
			}

		});
	</script>

	<div id="returndrug-p" class="easyui-panel" style="width: 100%; height: 100%; padding: 10px; background: #fafafa;"
		data-options="iconCls:'icon-save',closable:true,collapsible:true,minimizable:true,maximizable:true">

		<fieldset style="border: 1px solid #A6A6A6; border-radius: 6px; width: 1050px; margin: 0 auto; margin-top: 10px; margin-bottom: 20px;">
			
			<legend style="font-size: 17px">退药表</legend>
			
			<table id="returndrug-dg" class="logininform-table easyui-datagrid" style="width: 1030; height: 140px"
				data-options="toolbar:'#logininform-tb',fitColumns:false,singleSelect:false">

			</table>
		
		</fieldset>

		<fieldset style="border: 1px solid #A6A6A6; border-radius: 6px; width: 1050px; margin: 0 auto; margin-top: 10px; margin-bottom: 20px;">
			
			<legend style="font-size: 17px">退药记录</legend>
			
			<table id="returndrugjl-dg" class="logininform-table easyui-datagrid" style="width: 1030; height: 220px"
				data-options="toolbar:'#logininform-tb',fitColumns:false,singleSelect:false">

			</table>
		
		</fieldset>
	
	</div>
	
	<!-- 退药表详情 -->
	<div id="returndrug-win" class="easyui-window" title="退药表详情" style="width: 1080px; height: 600px"
		data-options="iconCls:'icon-save',modal:true,closed:true,shadow:true,resizable:false,collapsible:false,minimizable:false,maximizable:false">
		
		<div class="easyui-layout" data-options="fit:true">
			
			<div data-options="region:'center'">
				
				<div style="width: 1000; height: 60px;line-height:60px; padding: 10px; margin-left: 50px;">
					
					<p>
						<span style="margin-left: 60px;"></span>
						
						<input id="returndrug-tydh1" class="easyui-textbox" style="width: 200px" data-options="label:'退药单号：'"></input>
												
						<span style="margin-left: 60px;"></span>
						
						<input id="returndrug-yy" class="easyui-textbox" style="width: 200px" data-options="label:'原因：'"></input>
						
						<span style="margin-left: 60px;"></span> 
						
						<input id="returndrug-sj" class="easyui-textbox" style="width: 230px" data-options="label:'时间：'"></input>
						
						<input type="hidden" class="easyui-textbox"  id="asdfzt"></input>
					</p>
				
				</div>
				
				<div style="padding: 10px;margin-left:10px;">
					
					<table id="returndrugxq-dg" class="logininform-table easyui-datagrid" style="width: 1030px; height: 308px"
						data-options="toolbar:'#logininform-tb',fitColumns:false,singleSelect:false">

					</table>
				
				</div>
				
				<p style="margin-left: 300px;">
					
					<a id="returndrugok" style="width: 150px;" class="easyui-linkbutton" iconCls="icon-ok">确认退货</a> 
					
					<span style="margin-left: 50px;"></span> 
					
					<a id="hsdfdf" class="easyui-linkbutton" style="width: 150px;" iconCls="icon-cancel">取消</a>
				
				</p>
			
			</div>
		
		</div>
	
	</div>

	<!-- 退药记录详情 -->
	<div id="returndrugjlxq-win" class="easyui-window" title="退药详情" style="width: 1080px; height: 500px"
		data-options="iconCls:'icon-save',modal:true,closed:true,shadow:true,resizable:false,collapsible:false,minimizable:false,maximizable:false">
		
		<div class="easyui-layout" data-options="fit:true">
			
			<div data-options="region:'center'">
				
				<div style="width: 1000; height: 60px;line-height:60px; padding: 10px; margin-left: 50px;">
					
					<p>
						<span style="margin-left: 60px;"></span>
						
						<input id="returndrug-tydh" class="easyui-textbox" style="width: 200px" data-options="label:'退药单号：',readonly:true"></input>
												
						<span style="margin-left: 60px;"></span>
					
						<input id="returndrug-yy1" class="easyui-textbox" style="width: 200px" data-options="label:'原因：',readonly:true"></input>						
						
						<span style="margin-left: 60px;"></span> 
						
						<input id="returndrug-tysj" class="easyui-textbox" style="width: 230px" data-options="label:'退药时间：',readonly:true"></input>
					
					</p>
			
				</div>
				
				<div style="padding: 10px;margin-left:10px">
					
					<table id="returndrugjlxq-dg" class="logininform-table easyui-datagrid" style="width: 1030px; height: 308px"
						data-options="toolbar:'#logininform-tb',fitColumns:false,singleSelect:false">
					
					</table>
				
				</div>
			
			</div>
		
		</div>
	
	</div>

	<input type="hidden" value="${user.userName}"  id="checkpeople"> 
	
	<input type="hidden" value="${user.userName}"  id="indentPeople">
	<!-- 弹框提示 -->
	<div id="DRUGONEu" class="easyui-window"  title="提示框" style="width:300px;height:156px"
	    data-options="iconCls:'icon-save',modal:true,resizable:false,collapsible:false,maximizable:false,minimizable:false,closed:true">
	          <p class="drugoneu" style="text-align:center;font-size: 16px;margin-top: 47px;">
	          </p>
	</div>
	
</body>
</html>